//
//  AFHttpTool.m
//  RCloud_liv_demo
//
//  Created by Liv on 14-10-22.
//  Copyright (c) 2014年 RongCloud. All rights reserved.
//


#import "AFHttpTool.h"



#define LOGIN_URL @"get_login_user.do"
//#define ContentType @"text/plain"
#define ContentType @"text/html"

@implementation AFHttpTool

+ (AFHTTPSessionManager* )requestWihtMethod:(RequestMethodType)methodType
                      baseurl:(NSString*)baseurl
                      url:(NSString*)url
                   params:(NSDictionary*)params
                  success:(void (^)(id response))success
                  failure:(void (^)(NSError* err))failure
{
    NSURL* baseURL = [NSURL URLWithString:baseurl];
    //获得请求管理者
    AFHTTPSessionManager* mgr = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",ContentType, nil];
    mgr.requestSerializer.timeoutInterval = 10.f;
    //mgr.responseSerializer=[AFJSONResponseSerializer serializer];
//    NSString *cookieString = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserCookies"];
//
//    if(cookieString)
//       [mgr.requestSerializer setValue: cookieString forHTTPHeaderField:@"Cookie"];
 
    switch (methodType) {
        case RequestMethodTypeGet:
        {
            //GET请求
            [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    success(responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure(error);
                }
            }];

        }
            break;
        case RequestMethodTypePost:
        {
            [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                if (success) {
                    
                    success(responseObject);
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure(error);
                }
            }];
        }
            break;
        default:
            break;
    }
    return mgr;
}

@end
